package com.bingfor.easycablelink.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.bingfor.easycablelink.R;


/**
 * Created by Modificator
 * Date: 2016/2/18
 * Time: 17:09
 * Connect: yunshangcn@gmail.com
 */
public abstract class BaseFragment extends com.trello.rxlifecycle.components.support.RxFragment {

    public BaseActivity mContext;
    public View rootView;
    protected boolean isPause = true;
    protected ProgressDialog waitDialog;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = (BaseActivity) getActivity();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rootView = rootView == null ? getView() : rootView;
        waitDialog = mContext.waitDialog;
    }

    public abstract void refresh();


    public abstract void initViews();

    public abstract void initToolbar();

    public void startActivity(Intent intent) {
        try {
            super.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void setTitle(String title) {
        TextView tvTitle = $(R.id.toolbar);
        if (title != null) {
            tvTitle.setText(title);
        }
    }

    /**
     * 跳转actiivty
     *
     * @param c      要跳转的activity class
     * @param bundle 跳转时传递的参数
     */
    public void moveToNextPage(@NonNull Class<?> c, Bundle bundle) {
        moveToNextPage(c, bundle, 0);
    }

    /**
     * 跳转activity class
     *
     * @param c      要跳转的activity class
     * @param bundle 跳转时传递的参数
     * @param flags  The desired flags.{@link Intent#setFlags(int)}
     */
    public void moveToNextPage(@NonNull Class<?> c, Bundle bundle, int flags) {
        moveToNextPage(c, bundle, false, flags);
    }

    /**
     * 跳转activity class
     *
     * @param c      要跳转的activity class
     * @param bundle 跳转时传递的参数
     * @param b      是否需要继续传递前一个页面传过来的参数
     * @param flags  The desired flags.{@link Intent#setFlags(int)}
     */
    public void moveToNextPage(@NonNull Class<?> c, Bundle bundle, boolean b, int flags) {
        mContext.moveToNextPage(c, bundle, b, flags);
    }

    /**
     * 跳转activity class
     *
     * @param c 要跳转的activity class
     */
    public void moveToNextPage(Class<?> c) {
        moveToNextPage(c, null);
    }

    /**
     * 跳转activity class
     *
     * @param c     要跳转的activity class
     * @param flags The desired flags.{@link Intent#setFlags(int)}
     */
    public void moveToNextPage(Class<?> c, int flags) {
        moveToNextPage(c, null, flags);
    }

    public <T extends View> T $(int id) {
        return (T) rootView.findViewById(id);
    }

    /**
     * 获取View对象
     *
     * @param id  View的ID
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int id) {
        return $(id);
    }

    @Override
    public void onResume() {
        super.onResume();
        isPause = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    public void onDestroy() {
        isPause = true;
        super.onDestroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //TODO 考虑是否也判断当前页面是否在后台
    }

}
